package gw.com.android.ui;

import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.gwtsz.gts2.hx.R;

import java.util.ArrayList;

import butterknife.BindView;
import gw.com.android.app.ActivityManager;
import gw.com.android.model.ConfigType;
import gw.com.android.utils.ImageUtil;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;
import www.com.library.util.StateListUtil;
import www.com.library.view.ScrollPoints;
import www.com.library.view.TintTextView;

/**
 * 新手帮助页
 */
public class UserHelpActivity extends BaseActivity implements OnClickListener {

    @BindView(R.id.scrollPoints)
    ScrollPoints scrollPoints; // 翻页指示器
    @BindView(R.id.userhelp_page)
    ViewPager viewPager;

    private boolean hasBtn = true;
    private ArrayList<View> pageViews;
    private Bitmap bitmap1, bitmap2, bitmap3, bitmap4, bitmap5;
    private int mCurrentPage = 0;
    private String mTitle;

    @Override
    protected void setAcitivityParam() {
        hasBtn = getIntent().getBooleanExtra("hasBtn", true);
        mTitle = getIntent().getStringExtra("mTitle");
        if (hasBtn) {
            // 全屏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    protected int getLayoutView() {
        return R.layout.activity_userhelp;
    }

    @Override
    protected void initLayoutView() {
        mTitleBar.setAppTitle(mTitle);
        if (hasBtn) {
            mTitleBar.setVisibility(View.GONE);
        } else {
            mTitleBar.setVisibility(View.VISIBLE);

        }
        scrollPoints.setPointMargin(DeviceUtil.instance().dip2px(3, getApplicationContext()));
        scrollPoints.initPoints(this, 5, 0, R.mipmap.a_gdpage_lip, R.mipmap.a_gdpage_lip_p, false);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                scrollPoints.changeSelectedPoint(arg0);
                mCurrentPage = arg0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // 状态有三个0空闲，1是增在滑行中，2目标加载完毕
                int SCROLL_STATE_IDLE = 0;
                int SCROLL_STATE_DRAGGING = 1;
                int SCROLL_STATE_SETTLING = 2;
                if (arg0 == SCROLL_STATE_IDLE) {
                    if (hasBtn && mCurrentPage == 4) {
                        scrollPoints.setVisibility(View.GONE);
                    } else {
                        scrollPoints.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    @Override
    protected void initViewData() {
        pageViews = new ArrayList<>();
        LayoutInflater inflater = getLayoutInflater();

        View view1 = inflater.inflate(R.layout.list_userhelp_item, null);
        ImageView imageView = (ImageView) view1.findViewById(R.id.imageview);
        TintTextView startBtn1 = (TintTextView) view1.findViewById(R.id.startbtn);
        startBtn1.setVisibility(View.GONE);
        bitmap1 = ImageUtil.getBitmapFromResource(R.mipmap.a_gdpage_001);
        imageView.setImageBitmap(bitmap1);
//		ImageUtil.scaleImage(this, imageView, R.mipmap.a_gdpage_001);
        pageViews.add(view1);

        View view2 = inflater.inflate(R.layout.list_userhelp_item, null);
        ImageView imageView2 = (ImageView) view2.findViewById(R.id.imageview);
        TintTextView startBtn2 = (TintTextView) view2.findViewById(R.id.startbtn);
        startBtn2.setVisibility(View.GONE);
        bitmap2 = ImageUtil.getBitmapFromResource(R.mipmap.a_gdpage_002);
        imageView2.setImageBitmap(bitmap2);
//		ImageUtil.scaleImage(this, imageView2, R.mipmap.a_gdpage_002);
        pageViews.add(view2);

        View view3 = inflater.inflate(R.layout.list_userhelp_item, null);
        ImageView imageView3 = (ImageView) view3.findViewById(R.id.imageview);
        TintTextView startBtn3 = (TintTextView) view3.findViewById(R.id.startbtn);
        startBtn3.setVisibility(View.GONE);
        bitmap3 = ImageUtil.getBitmapFromResource(R.mipmap.a_gdpage_003);
        imageView3.setImageBitmap(bitmap3);
//		ImageUtil.scaleImage(this, imageView3, R.mipmap.a_gdpage_003);
        pageViews.add(view3);

        View view4 = inflater.inflate(R.layout.list_userhelp_item, null);
        ImageView imageView4 = (ImageView) view4.findViewById(R.id.imageview);
        TintTextView startBtn4 = (TintTextView) view4.findViewById(R.id.startbtn);
        startBtn4.setVisibility(View.GONE);
        bitmap4 = ImageUtil.getBitmapFromResource(R.mipmap.a_gdpage_004);
        imageView4.setImageBitmap(bitmap4);
//		ImageUtil.scaleImage(this, imageView3, R.mipmap.a_gdpage_003);
        pageViews.add(view4);

        View view5 = inflater.inflate(R.layout.list_userhelp_item, null);
        ImageView imageView5 = (ImageView) view5.findViewById(R.id.imageview);
        TintTextView startBtn5 = (TintTextView) view5.findViewById(R.id.startbtn);
        startBtn5.setBackgroundDrawable(StateListUtil.getDrawList(this, R.mipmap.a_gdpage_btn, R.mipmap.a_gdpage_btn_p));
        startBtn5.setText(R.string.userhelp_start_title);
        startBtn5.setOnClickListener(this);
        bitmap5 = ImageUtil.getBitmapFromResource(R.mipmap.a_gdpage_005);
        imageView5.setImageBitmap(bitmap5);
        pageViews.add(view5);

        startBtn1.setVisibility(View.GONE);
        startBtn2.setVisibility(View.GONE);
        startBtn3.setVisibility(View.GONE);
        startBtn4.setVisibility(View.GONE);
        startBtn5.setVisibility(View.VISIBLE);

        UserPageAdapter mAdapter = new UserPageAdapter();
        viewPager.setAdapter(mAdapter);
    }

    private void recycle() {
        if (null != bitmap1 && bitmap1.isRecycled()) {
            bitmap1.recycle();
            bitmap1 = null;
        }

        if (null != bitmap2 && bitmap2.isRecycled()) {
            bitmap2.recycle();
            bitmap2 = null;
        }

        if (null != bitmap3 && bitmap3.isRecycled()) {
            bitmap3.recycle();
            bitmap3 = null;
        }

        if (null != bitmap4 && bitmap4.isRecycled()) {
            bitmap4.recycle();
            bitmap4 = null;
        }
        if (null != bitmap5 && bitmap5.isRecycled()) {
            bitmap5.recycle();
            bitmap5 = null;
        }
    }

    @Override
    public void finish() {
        super.finish();
        recycle();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recycle();
    }

    /**
     * 指引页面Adapter
     */
    public class UserPageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(pageViews.get(position));
            return pageViews.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(pageViews.get(position));
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (CommonUtils.isFastDoubleClick()) {
            return;
        }
        ActivityManager.showMainTab(UserHelpActivity.this, ConfigType.TAB_HOME_TAG, 0);
    }

}
